package com.efesco.common;

import com.bstek.dorado.common.Setting;
import com.bstek.dorado.common.VelocityHelper;
import com.bstek.dorado.utils.StringHelper;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

import com.bstek.dorado.action.Action;
import com.bstek.dorado.action.Controller;
import com.bstek.dorado.action.mapping.ActionForward;

public class ExceptionController extends Controller{
	public ActionForward doAction(Action action, HttpServletRequest httpservletrequest,
			HttpServletResponse httpservletresponse) throws Exception {
		
	    
	        Template template = VelocityHelper.getTemplate("com/common/exception.html");
	        VelocityContext velocitycontext;
	        (velocitycontext = new VelocityContext()).put("request", httpservletrequest);
	        Throwable throwable;
	        String s;
	        if(StringHelper.isNotEmpty(s = (throwable = (Throwable)httpservletrequest.getAttribute("com.common.AppException")).getMessage()))
	            velocitycontext.put("message", s);
	        else
	            velocitycontext.put("message", throwable.getClass().getName());
	        CharArrayWriter chararraywriter = new CharArrayWriter();
	        PrintWriter printwriter = new PrintWriter(chararraywriter, true);
	        throwable.printStackTrace(printwriter);
	        String s1 = chararraywriter.toString();
	        StringTokenizer stringtokenizer;
	        if((stringtokenizer = new StringTokenizer(s1, "\r\n")).hasMoreTokens())
	            velocitycontext.put("firstStackTrace", stringtokenizer.nextToken());
	        ArrayList arraylist = new ArrayList();
	        for(; stringtokenizer.hasMoreTokens(); arraylist.add(stringtokenizer.nextToken()));
	        velocitycontext.put("stackTraces", arraylist);
	        String s2 = "text/html; charset=" + Setting.getString("common.defaultCharsetName");
	        httpservletresponse.setContentType(s2);
	        httpservletresponse.addHeader("cache-control", "no-cache");
	        PrintWriter printwriter1 = httpservletresponse.getWriter();
	        template.merge(velocitycontext, printwriter1);
	        return null;
	    
	}	
}
